home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d16 / nodee1a.arc / SOURCE.ARC / CLASSWIN.H < prev    next >
C/C++ Source or Header  |  1991-09-20  |  10KB  |  318 lines

  1. /*---------------------------------------------------------------------
  2.  
  3.     CLASSWIN.H
  4.  
  5.             Copyright (c) 1991 by Borland International
  6.             All Rights Reserved.
  7.  
  8.     Although they don't say anything about it, this class library
  9.     was included with the example files in Borland C++ 2.0.
  10.  
  11.     This class is a much easier to use than the WHELLLO.CPP
  12.     example which the manuals talk about.
  13.  
  14.     Defines the following classes, which are useful for building
  15.     general purpose windows applications:
  16.  
  17.     WinBase - provides basic data and functionality for
  18.                         windows programming.
  19.  
  20.     ModalDialog - provides for modal dialog boxes.  Inherits from
  21.                         WinBase, which is a virtual base.
  22.  
  23.     Window  - provides core functionality for a window under Windows.
  24.                         Inherits from WinBase, which is a virtual base.
  25.  
  26.  
  27.     ChildWindow - base class for child window classes
  28.  
  29.  
  30. ---------------------------------------------------------------------*/
  31.  
  32.  
  33. // Continue only if this header has not already been compiled
  34.  
  35.     #if !defined ( __CLASSWIN_H )
  36.     #define __CLASSWIN_H
  37.  
  38. /*---------------------------------------------------------------------
  39.  
  40.     class WinBase
  41.  
  42.             provides the basic data and functionality for all classes
  43.             used in the windows application.  It is an abstract base class.
  44.  
  45.     public:
  46.  
  47.         virtual WORD run() = 0;     // the core function of all windows!  For
  48.                                                                 // the main application window, this
  49.                                                                 // provides the message loop.  In modal
  50.                                                                 // dialogs, it sets up the dialog box,
  51.                                                                 // calls the dialog proc, and closes down
  52.                                                                 // the dialog.
  53.  
  54.  
  55.  
  56.         static  HANDLE hInst;       // the handle of the current instance
  57.  
  58.         static  HANDLE hPrevInst;   // the handle of the previous instance
  59.  
  60.         static  LPSTR cmd;          // pointer to the command line
  61.  
  62.         static  int show;           // the nCmdShow parameter of the current
  63.                                                                 // instance
  64.  
  65.         HANDLE  hWnd();             // access function
  66.  
  67.     protected:
  68.  
  69.         HWND hWindow;               // the window handle of the class.  This is
  70.                                                                 // accessed through hWnd(), and it will
  71.                                                                 // provide the correct handle for any
  72.                                                                 // derived class.
  73.  
  74.                                                                 // NOTE: this field is not initialized by
  75.                                                                 // the constructor of this class.  It must
  76.                                                                 // be initialized by the constructor of a
  77.                                                                 // class derived from this class.
  78.  
  79. ---------------------------------------------------------------------*/
  80.  
  81.     class WinBase
  82.     {
  83.  
  84.     public:
  85.         virtual WORD run() = 0;
  86.         static  HANDLE hInst;
  87.         static  HANDLE hPrevInst;
  88.         static  LPSTR cmd;
  89.         static  int show;
  90.         HANDLE  hWnd();
  91.  
  92.     protected:
  93.         HWND hWindow;
  94.     };
  95.  
  96.  
  97.     inline HANDLE WinBase::hWnd()
  98.     {
  99.         return hWindow;
  100.     }
  101.  
  102. /*---------------------------------------------------------------------
  103.  
  104.     class ModalDialog
  105.  
  106.             provides basic functionality for modal dialog boxes.  It is an
  107.             abstract base class.
  108.  
  109. ---------------------------------------------------------------------*/
  110.  
  111.     class ModalDialog : public virtual WinBase
  112.     {
  113.     public:
  114.  
  115.         ModalDialog( HWND );        // constructor.  Since a modal dialog
  116.                                                                 // needs to know its owner, the handle
  117.                                                                 // of the owner is passed as a parameter
  118.                                                                 // to the constructor.
  119.  
  120.         ~ModalDialog();
  121.  
  122.         virtual WORD run();         // the core of a modal dialog!  It sets
  123.                                                                 // up the dialog, executes it, and closes
  124.                                                                 // it down.
  125.  
  126.     protected:
  127.  
  128.         virtual BOOL dispatch( HWND, WORD, WORD, LONG );
  129.                                                                 // the core of any window.  Whenever
  130.                                                                 // dlgProc receives a message it passes
  131.                                                                 // the message on to dispatch().  If
  132.                                                                 // dispatch() doesn't handle the message
  133.                                                                 // itself, it should call the dispatch()
  134.                                                                 // function in its base class.  Ultimately,
  135.                                                                 // messages not handled higher up are
  136.                                                                 // handled by this version of dispatch(),
  137.                                                                 // which just returns FALSE, indicating
  138.                                                                 // that the message wasn't handled by
  139.                                                                 // the dialog box at all.
  140.  
  141.                         WORD result;        // this holds the value which will be
  142.                                                                 // returned by run().  If the dialog box
  143.                                                                 // needs to return a success code, that
  144.                                                                 // code should be stored here by the
  145.                                                                 // dialog handler.
  146.  
  147.     private:
  148.  
  149.     virtual LPSTR getDialogName() = 0;
  150.                                 // returns a far pointer to a string
  151.                                 // that contains the name of the dialog
  152.                                 // resource for the current dialog box.
  153.                                 // This is used in run() to load the
  154.                                 // resource.
  155.  
  156.     static  BOOL FAR PASCAL _export dlgProc( HWND, WORD, WORD, LONG );
  157.                                 // the dialog proc that windows calls
  158.                                 // when it has messages for the current
  159.                                 // dialog.
  160.  
  161.     static  ModalDialog *curDlg;
  162.                                 // this holds a pointer to the currenly
  163.                                 // active ModalDialog.  It is set up when
  164.                                 // the constructor is called, and is used
  165.                                 // by dlgProc() to route messages to the
  166.                                 // right dispatch() function.
  167.  
  168.     };
  169.  
  170.     inline ModalDialog::ModalDialog( HWND hOwner ) : result( 0 )
  171.     {
  172.         curDlg = this;
  173.         hWindow = hOwner;
  174.     }
  175.  
  176.     inline ModalDialog::~ModalDialog()
  177.     {
  178.         curDlg = 0;
  179.     }
  180.  
  181.  
  182.  
  183. /*---------------------------------------------------------------------
  184.     class Window
  185.  
  186.          provides the basic functionality for a normal window under
  187.          windows.
  188.  
  189. public:
  190.  
  191.  
  192.         virtual BOOL create();      // creates the window.  This involves
  193.                                                                 // registering the window class if
  194.                                                                 // it hasn't already been registered,
  195.                                                                 // creating the actual window, and
  196.                                                                 // inserting the window into the internal
  197.                                                                 // window list.
  198.         virtual WORD run();         // provides the message loop.
  199.  
  200. protected:
  201.  
  202.         virtual LONG dispatch( WORD, WORD, LONG );
  203.                                 // dispatches messages in a class derived
  204.                                                                 // from Window.  When wndProc() receives
  205.                                 // a message, it determines which Window
  206.                                 // object the message should be routed
  207.                                                                 // to, and calls dispatch() for that object.
  208.                                 // If the dispatch() function in a derived
  209.                                                                 // class does not handle any particular
  210.                                                                 // message, it should pass that message
  211.                                                                 // on down to the dispatch() function in
  212.                                                                 // its base class.  The version of dispatch()
  213.                                                                 // in Window itself just calls DefWindowProc.
  214.  
  215.         virtual BOOL registerClass() = 0;
  216.                                                                 // the derived class should override this
  217.                                                                 // function and register a window class
  218.                                                                 // defined appropriately for the program.
  219.  
  220.         virtual BOOL createWindow() = 0;
  221.                                                                 // the derived class should override this
  222.                                                                 // function and create a window that's
  223.                                                                 // appropriate for the program.
  224.  
  225.         static  LONG FAR PASCAL _export wndProc( HWND, WORD, WORD, LONG );
  226.                                                                 // the window proc that windows calls
  227.                                                                 // when it has messages for any window
  228.                                                                 // in the current application.  wndProc()
  229.                                                                 // posts the message to the appropriate
  230.                                                                 // window.
  231.  
  232.                         void insert();      // should be called from createWindow()
  233.                                                                 // after successfully calling the windows
  234.                                                                 // function CreateWindow().  insert() adds
  235.                                                                 // the current object to the Window list,
  236.                                                                 // enabling normal dispatching of messages
  237.                                                                 // to the current object.
  238.  
  239. private:
  240.  
  241.         static Window *winList;     // have to maintain a list of Windows so
  242.         Window *nextWin;            // that we can dispatch messages to the
  243.                                                                 // correct one.
  244.  
  245.         static Window *inCreate;    // sort-of kludgy.  But there are messages
  246.                                                                 // that are sent to a window during the
  247.                                                                 // call to CreateWindow().  We assume that
  248.                                                                 // any message received by wndProc() during
  249.                                                                 // a call to CreateWindow() is meant for
  250.                                                                 // the window being created, and dispatch
  251.                                                                 // those messages to that window.
  252.  
  253.  
  254. ---------------------------------------------------------------------*/
  255.  
  256.     class Window : public virtual WinBase
  257.     {
  258.  
  259.     public:
  260.  
  261.  
  262.         virtual BOOL create();
  263.         virtual WORD run();
  264.  
  265.     protected:
  266.  
  267.         virtual LONG dispatch( WORD, WORD, LONG );
  268.         virtual BOOL registerClass() = 0;
  269.         virtual BOOL createWindow() = 0;
  270.         static  LONG FAR PASCAL _export wndProc( HWND, WORD, WORD, LONG );
  271.                         void insert();
  272.  
  273.     private:
  274.  
  275.         static Window *winList;
  276.         Window *nextWin;
  277.         static Window *inCreate;
  278.  
  279.     };
  280.  
  281.     inline void Window::insert()
  282.     {
  283.         nextWin = winList;
  284.         winList = this;
  285.     }
  286.  
  287.  
  288. /*-------------------------------------------
  289. class Child Windows
  290.  
  291. Other child windows can derive from this class.
  292. This class contains the parents handle and the childs handle.
  293.  
  294. public:
  295.     HWND getHandle(void) - returns the childs handle
  296.  
  297.  
  298. ------------------------------------------------------*/
  299.  
  300.     class Child
  301.     {
  302.     public:
  303.         HWND getHandle(void);
  304.  
  305.     protected:
  306.         HWND hwndChild;
  307.         HWND hParent;
  308.  
  309.     };
  310.  
  311.     inline HWND Child::getHandle()
  312.     {
  313.         return hwndChild;
  314.     }
  315.  
  316.  
  317.     #endif
  318.